package 链家内推;

import java.io.*;
import java.util.ArrayList;

public class FileIO {
    public static void main(String[] args) throws IOException {

        /**
         * 输入操作
         */

        //按照行读取
        BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("D:\\a.txt")));
        String line;
        ArrayList<String> list = new ArrayList<>();
        while ((line = br.readLine()) != null) {
            list.add(line);
        }
        br.close();
        //打印
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i));
        }

        /**
         * 输出操作
         */

        //字节流转字符流
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("D:\\b.txt")));
        for (int i = 0; i < list.size() - 1; i++) {
            bw.write(list.get(i));
            bw.newLine();
        }
        bw.write(list.get(list.size() - 1));
        bw.close();

        //文件写
        BufferedWriter bw1 = new BufferedWriter(new FileWriter("D:\\d.txt"));
        for (int i = 0; i < list.size() - 1; i++) {
            bw1.write(list.get(i));
            bw1.newLine();
        }
        bw1.write(list.get(list.size() - 1));
        bw1.close();

        //windows上的输出换行，字节流
        FileOutputStream fos = new FileOutputStream("D:\\c.txt");
        for (int i = 0; i < list.size() - 1; i++) {
            fos.write(list.get(i).getBytes());
            fos.write("\r\n".getBytes());
        }
        fos.write(list.get(list.size() - 1).getBytes());
        fos.close();
    }
}
